$(function () {
    let commonwealId = getQueryString("commonwealId");
    let url = '../donationrecord/list';
    if (commonwealId) {
        url += '?isPay=1&commonwealId=' + commonwealId;
    }
    $("#jqGrid").jqGrid({
        url: url,
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true, align: "center"},
            // {label: '用户id', name: 'userId', index: 'user_id', width: 80, align: "center"},
            {label: '用户昵称', name: 'userNickname', index: 'user_nickname', width: 80, align: "center"},
            // {label: '公益活动id', name: 'commonwealId', index: 'commonweal_id', width: 80, align: "center"},
            {label: '公益活动名称', name: 'commonwealName', index: 'commonweal_name', width: 80, align: "center"},
            {label: '捐赠金额', name: 'donorAmount', index: 'donor_amount', width: 80, align: "center"},
            {
                label: '是否已支付', name: 'isPay', index: 'is_pay', width: 80, align: "center",
                formatter: function (value) {
                    if (value === 1) {
                        return '<span class="label label-success">是</span>';
                    } else if (value === 0) {
                        return '<span class="label label-danger">否</span>';
                    }
                }
            },
            {label: '商户订单号', name: 'outTradeNo', index: 'out_trade_no', width: 80, align: "center"},
            // {label: '微信支付订单号', name: 'transactionId', index: 'transaction_id', width: 150, align: "center"},
            {label: '捐赠者姓名', name: 'donorName', index: 'donor_name', width: 80, align: "center"},
            {
                label: '是否匿名', name: 'isAnonymous', index: 'is_anonymous', width: 80, align: "center",
                formatter: function (value) {
                    return value === 1 ? '是' : '否';
                }
            },
            {
                label: '创建时间', name: 'addTime', index: 'add_time', width: 80, align: "center",
                formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd hh:mm:ss');
                }
            }],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        donationRecord: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.donationRecord = {};
        },
        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            let url = vm.donationRecord.id == null ? "../donationrecord/save" : "../donationrecord/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(vm.donationRecord),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows();
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../donationrecord/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../donationrecord/info/" + id, function (r) {
                vm.donationRecord = r.donationRecord;
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});